package com.ruoyi.common.baidu.retry;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dtflys.forest.callback.RetryWhen;
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.http.ForestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 百度请求重试
 *
 * @author liaowen
 * @date 2024/11/8 14:21
 */
@Slf4j
@Component
public class BaiduCommonRetryWhen implements RetryWhen {
    @Override
    public boolean retryWhen(ForestRequest forestRequest, ForestResponse forestResponse) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(forestResponse.getContent());
            if (ObjectUtil.isNotNull(jsonObject.getString("error_code"))) {
                log.info("请求重试：url = " + forestRequest.getUrl() + " ----- result = " + forestResponse.getContent());
                return true;
            }
            return false;
        } catch (Exception e) {
            log.error("请求重试失败：", e);
            return false;
        }
    }
}
